package com.space.user.repositoryimpl;

import com.space.user.converter.UserInfoConverter;
import com.space.user.database.dao.UserInfoDao;
import com.space.user.database.dataobject.UserInfoDO;
import com.space.user.domain.info.entity.UserInfo;
import com.space.user.domain.info.repository.InfoRepository;
import com.space.user.domain.info.valueobject.UserId;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/4/15 4:23 下午
 */
@Repository
public class InfoRepositoryImpl implements InfoRepository {
    @Resource
    UserInfoDao userInfoDao;

    @Autowired
    UserInfoConverter userInfoConverter;

    @Override
    public @NonNull UserInfo find(UserId userId) {
        return userInfoConverter.toEntity(userInfoDao.selectById(userId.getUserId()));
    }

    @Override
    public UserInfo save(UserInfo userInfo) {
        UserInfoDO userInfoDO = userInfoConverter.toDO(userInfo);
        if (Objects.isNull(userInfo.getUserId())) {
            userInfoDao.insert(userInfoDO);
        } else {
            userInfoDao.updateById(userInfoDO);
        }

        return userInfoConverter.toEntity(userInfoDO);
    }
}
